import {
  detail
} from '../../../js/modules/mall';
import {
  cart
} from "../../../js/modules/order.js"
import {
  api
} from '../../../js/util'
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  behaviors: [componentBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      value: {}
    },
    auxiliaryUnits:{
      type:Array,
      value:[]
    },
    idx:{
      type:Number,
      value:0
    },
    primaryKey: {
      type: String,
      value: ""
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    align:'left',
    valign:'bottom',
    opacityStyle: 'opacity-0',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onUnitShowEvent(e) {
      let that = this
      let x,y, creenWidth,creenHeight;
      x = e.changedTouches[0].clientX;    //unit组件相对页面的x坐标
      y = e.changedTouches[0].clientY;    //unit组件相对页面的y坐标
      creenWidth = wx.getSystemInfoSync().windowWidth;
      creenHeight = wx.getSystemInfoSync().windowHeight;
      let query = this.createSelectorQuery()
      // console.log(x,creenWidth*0.5,creenWidth*0.3)
      if (y <= creenHeight*0.8){        //如果y坐标小于等于页面宽度的0.8，这内容在下方显示
        this.setData({
          valign:'bottom'
        })
      }else{
        this.setData({
          valign: 'top'
        })
      }


      if (this.data.auxiliaryUnits.length==1)return;
      let g = this.data.item;
      g.ShowUnit = !(g.ShowUnit == undefined ? false : g.ShowUnit);
      this.setData({
        item:g,
        opacityStyle: 'opacity-0'
      }, function () {
        // 动态获取元素宽度然后再判断显示位置
        query.select('.more-unit-cont').fields({
          size: true
        }).exec((res) => {
          let width = res[0].width
          let align = 'center'
          if (width) {
            if (x - width < 0) {
              if (x + width < creenWidth) {
                align = 'left'
              } else {
                align = 'center'
              }
            } else {
              // center向左偏移了四分之一，所以要计算宽度/4
              if ((x - (width / 4)) > 0) {
                align = 'center'
              } else {
                align = 'right'
              }
            }
            that.setData({
              align
            }, function () {
              // 到下个时间片再显示，防止同时显示和调整位置导致区域跳动的问题
              wx.nextTick(() => {
                that.setData({
                  opacityStyle: 'opacity-1'
                })
              })
            })
          } else {
            // 到下个时间片再显示，防止同时显示和调整位置导致区域跳动的问题
            wx.nextTick(() => {
              that.setData({
                opacityStyle: 'opacity-1'
              })
            })
          }
        })
      })
      // this.triggerEvent("onAddCartCompleted", { product: g, idx: this.data.idx, primaryKey: this.data.primaryKey});
    },
    /**
   * 改变单位
   */
    onChangeGoodsUnitEvent: function (e) {
      let g = this.data.item;
      let { idx} = e.currentTarget.dataset;
      let unit = this.data.auxiliaryUnits[idx];
      g.Translated = unit.Translated;
      g.DefaultUnit = unit.Name;
      g.AuxUnit = unit.Name;
      g.ZIndex = idx;
      g.ShowUnit = false;
      g.choose = true;
      g.CartCount = unit.Qty;
      g.BuyLowerLimit = unit.BuyLowerLimit;
      g.BuyUpperLimit = unit.BuyUpperLimit;
      g.Stock = unit.Stock;
      this.setData({
        item: g,
        opacityStyle: 'opacity-0'
      })
      //获得阶梯价格
      detail.getFastUnitPrice(g, g.CartCount, g.Translated==1);
      this.triggerEvent("onAddCartCompleted", { product: g, idx: this.data.idx, primaryKey: this.data.primaryKey });
    },
  }
})
